package com.ztgf.shopping.api.cms.epg;

import cn.afterturn.easypoi.view.EasypoiPDFTemplateView;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.shopping.models.bo.epg.EpgBannerBO;
import com.ztgf.shopping.models.bo.epg.EpgBannerSaveBO;
import com.ztgf.shopping.models.dto.epg.EpgBannerDTO;
import com.ztgf.shopping.models.service.epg.IEpgBannerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * banner后台接口类
 *
 * @author mxli
 * @Date 2019-11-30 01:47:53
 */

@RestController
@RequestMapping("/cms/epgBanner")
@Slf4j
@Api(value = "EpgBannerController",tags = "Banner表接口")
public class EpgBannerController {
    @DubboReference
    private IEpgBannerService epgBannerService;

    /**
     * banner列表 根据功能id
     * @param moduleId 功能id
     * @return
     */
    @ApiOperation(value = "Banner列表 根据功能id",notes = "Banner列表 根据功能id")
    @PostMapping("/getListByModuleId")
    public Result<List<EpgBannerDTO>> getListByModuleId(Long moduleId){
        EpgBannerBO epgBannerBO = new EpgBannerBO();
        epgBannerBO.setModuleId(moduleId);
        List<EpgBannerDTO> epgBannerDTOList = epgBannerService.getBySelectBannerInfoNoPage(epgBannerBO);
        return new Result<>(epgBannerDTOList);
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存Banner表",notes = "保存Banner表")
    public Result save( EpgBannerSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "添加Banner表",notes = "添加Banner表")
    @PostMapping(value = "/insert")
    public Result insert( EpgBannerSaveBO insertSaveBO) {
        boolean flag = epgBannerService.add(insertSaveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @ApiOperation(value = "修改Banner表",notes = "修改Banner表")
    @PostMapping("/update")
    public Result update( EpgBannerSaveBO updateSaveBO){
        EpgBannerDTO epgBannerDTO = new EpgBannerDTO();
        BeanUtils.copyProperties(updateSaveBO, epgBannerDTO);
        epgBannerDTO.setId(updateSaveBO.getId());
        boolean flag = epgBannerService.updateById(epgBannerDTO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @ApiOperation(value = "删除Banner表",notes = "删除Banner表")
    @ApiImplicitParam(name = "id",value = "Banner表id",required = true,paramType = "query",dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id){
        boolean flag = epgBannerService.deleteById(id);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除Banner表",notes = "批量删除Banner表")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        epgBannerService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();

    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找Banner表",notes = "根据id查找Banner表")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<EpgBannerDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        EpgBannerDTO epgBannerDTO = epgBannerService.selectById(id);
        return new Result<>(epgBannerDTO);
    }

    @ApiOperation(value = "Banner列表",notes = "Banner列表")
    @PostMapping("/querySelectPageList")
    public Result<Page<EpgBannerDTO>> querySelectPageList( EpgBannerBO epgBannerBO){
        if(null == epgBannerBO)
            return new Result<>(CodeEnum.FAIL_100002 );
        Page<EpgBannerDTO> page = epgBannerService.getBySelectBannerInfoPage(epgBannerBO);
        return new Result<>(page);
    }
}
